<?php
/*
 * 地区管理
 * @author     Mark <zhaody901@126.com>
 * @copyright  Copyright (c) 2013 lemon team
 * @version    $Id: 22d33d5b6f01e3eeef5cb9097b852d470c869061 $
 * @time       14-2-7 上午10:03
 */
defined('IN_IXDCW') or exit('Access Denied');

$AREA         = PCache::read('area.php');
$areaid       = isset($areaid) ? intval($areaid) : 0;
$do           = new AreaModule($areaid);
$parentid     = isset($parentid) ? intval($parentid) : 0;
$this_forward = '?file=' . $file . '&parentid=' . $parentid;

$menus = array(
	array('地区添加', '?file=' . $file . '&action=add'),
	array('地区管理', '?file=' . $file),
	array('导入省市', '?file=' . $file . '&action=import', 'onclick="return confirm(\'确定导入中国省市数据吗？ 当前数据将被覆盖 \');"'),
	array('更新缓存', '?file=' . $file . '&action=cache'),
	array('修复区码', '?file=' . $file . '&action=code'),
	array('修复8位', '?file=' . $file . '&action=fullcode'),
	array('地区JSON', '?file=' . $file . '&action=areajs'),
);


switch ($action) {
	case 'add':
		if ($submit) {
			if (!$area['areaname']) msg('地区名不能为空');
			$area['areaname'] = trim($area['areaname']);
			if (strpos($area['areaname'], "\n") === false) {
				$do->add($area);
			} else {
				$areanames = explode("\n", $area['areaname']);
				foreach ($areanames as $areaname) {
					$areaname = trim($areaname);
					if (!$areaname) continue;
					$area['areaname'] = $areaname;
					$do->add($area);
				}
			}
			$do->repair();
			dmsg('添加成功', $this_forward);
		} else {
			include tpl('area_add');
		}
		break;
	case 'import':
		$file = DT_ROOT . '/file/setting/area.sql';
		is_file($file) or msg('数据文件不存在，请上传程序包内 file/setting/area.sql 文件至 file/setting 目录');
		require DT_INC . '/sql.func.php';
		sql_execute(File::get($file));
		PCache::sysArea();
		dmsg('导入成功', $this_forward);
		break;
	case 'cache':
		$do->repair();
		dmsg('更新成功', $this_forward);
		break;
	case 'code':
		$do->code(3421);
		dmsg('更新成功', $this_forward);
		break;
	case 'fullcode':
		$do->fullCode();
		dmsg('更新成功', $this_forward);
		break;
	case 'delete':
		if ($areaid) $areaids = $areaid;
		$areaids or msg();
		$do->delete($areaids);
		dmsg('删除成功', $this_forward);
		break;
	case 'update':
		if (!$area || !is_array($area)) msg();
		$do->update($area);
		dmsg('更新成功', $this_forward);
		break;
	default:
		$DAREA  = array();
		$result = $db->query("SELECT * FROM {$do->getTable()} WHERE parentid=$parentid ORDER BY listorder,areaid");
		while ($r = $db->fetchArray($result)) {
			$r['childs']         = substr_count($r['arrchildid'], ',');
			$DAREA[$r['areaid']] = $r;
		}
		include tpl('area');
		break;
}